package com.heifeng.cloudshelf.mvp.programme.presenter;

import androidx.viewpager.widget.ViewPager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.heifeng.cloudshelf.R;
import com.heifeng.cloudshelf.base.BaseResponse;
import com.heifeng.cloudshelf.base.RxPresenter;
import com.heifeng.cloudshelf.constants.HTTPConstants;
import com.heifeng.cloudshelf.dao.ProgrammeDetailBean;
import com.heifeng.cloudshelf.event.ImageClickEvent;
import com.heifeng.cloudshelf.helper.MyObserver;
import com.heifeng.cloudshelf.helper.RxUtil;
import com.heifeng.cloudshelf.mvp.programme.adapter.ProgrammeImageRvAdapter;
import com.heifeng.cloudshelf.mvp.programme.adapter.ViewpagerAdapter;
import com.heifeng.cloudshelf.mvp.programme.contract.ProgrammeDetailActivityContract;
import com.heifeng.cloudshelf.network.NetClient;
import com.heifeng.cloudshelf.utils.DensityUtils;
import com.heifeng.cloudshelf.utils.LoginErrorUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import io.reactivex.disposables.Disposable;

/**
 * Created by Administrator on 2019/9/15.
 */

public class ProgrammeDetailActivityPresenter extends RxPresenter<ProgrammeDetailActivityContract.View> implements ProgrammeDetailActivityContract.Presenter {
    private LinearLayoutManager layoutManager;

    private ProgrammeImageRvAdapter adapter;

    private List<String> imageList = new ArrayList<>();

    private int rvFirstVisibleItem = 0;

    private int rvLastVisibleItem = 0;

    @Inject
    public ProgrammeDetailActivityPresenter() {
    }


    @Override
    public void init() {
        if (null != mView) {


            initRecyclerView(mView.getRecyclerView());
            initViewPager(mView.getViewPager());

//        if (imageList.size() > 0)
//            Glide.with(mContext)
//                    .load(imageList.get(0))
//                    .into(mView.getIvMain());


            mView.getTvAllNum().setText("/" + imageList.size() + "张");
        }
        //setLongClickable是必须的
        //mView.getIvMain().setLongClickable(true);
        //mView.getIvMain().setOnTouchListener(new MyGestureListener(mContext));

    }

    public void initViewPager(ViewPager viewPager) {
        ViewpagerAdapter adapter1 = new ViewpagerAdapter(mContext, imageList);
        viewPager.setAdapter(adapter1);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                adapter.setSelectPos(position);
                mView.getTvSelect().setText("" + (position + 1));
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public List<String> getList() {
        return imageList;
    }

    @Override
    public int getFirstItemPos() {
        return rvFirstVisibleItem;
    }

    @Override
    public int getLastItemPos() {
        return rvLastVisibleItem;
    }

    @Override
    public void setImagesList(List<String> images) {
        this.imageList.clear();
        this.imageList = images;
    }

    @Override
    public void getTableModel(String id) {
        NetClient.getInstance().getNetApi().getTableModel(HTTPConstants.TABLE_MODEL, id, HTTPConstants.userId)
                .compose(RxUtil.<BaseResponse>rxSchedulers())
                .subscribe(new MyObserver<BaseResponse>(mContext) {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(BaseResponse baseResponse) {
                        Log.e("base", "mini:" + baseResponse.getMiniqrQr());
                        if (null != mView) {
                            mView.setCodeImage(baseResponse.getMiniqrQr());
                            mView.setViewImage(mView.getUrl(), "");
                            mView.setTitle(baseResponse.getTitle());
                        }
                    }
                });
    }

    @Override
    public void getProductionInfo(int productionInfoId) {
        NetClient.getInstance().getNetApi().getProductionInfo(productionInfoId)
                .compose(RxUtil.<BaseResponse<ProgrammeDetailBean>>rxSchedulers())
                .subscribe(new MyObserver<BaseResponse<ProgrammeDetailBean>>(mContext) {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(BaseResponse<ProgrammeDetailBean> baseResponse) {
                        if (0 == baseResponse.getCode()) {
                            imageList.clear();
                            imageList = baseResponse.getresult().getCloudImageList();
                            if (null != mView) {
                                init();
                                mView.setViewImage(baseResponse.getresult().getVrUrl(), baseResponse.getresult().getVideoUrl());
                                mView.setCodeImage(baseResponse.getresult().getMiniQrUrl());
                                mView.setTitle(baseResponse.getresult().getTitle());
                            }
//                            mView.setTitle(baseResponse.);
                        } else {
                            LoginErrorUtils.loginOut(mContext, baseResponse.getCode(), baseResponse.getmsg());
                        }
                    }
                });
    }

    public void initRecyclerView(RecyclerView recyclerView) {
//        imageList.add("http://pic16.nipic.com/20111006/6239936_092702973000_2.jpg");
//        imageList.add("http://pic25.nipic.com/20121112/9252150_150552938000_2.jpg");
//        imageList.add("http://pic26.nipic.com/20121221/9252150_142515375000_2.jpg");
//        imageList.add("http://pic13.nipic.com/20110409/7119492_114440620000_2.jpg");
//        imageList.add("http://pic68.nipic.com/file/20150601/8164280_104301508000_2.jpg");
//        imageList.add("http://pic16.nipic.com/20111006/6239936_092702973000_2.jpg");
//        imageList.add("http://pic25.nipic.com/20121112/9252150_150552938000_2.jpg");
//        imageList.add("http://pic26.nipic.com/20121221/9252150_142515375000_2.jpg");
//        imageList.add("http://pic13.nipic.com/20110409/7119492_114440620000_2.jpg");
//        imageList.add("http://pic68.nipic.com/file/20150601/8164280_104301508000_2.jpg");

        if (imageList.size() > 7) {
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            layoutParams.width = DensityUtils.dp2px(mContext, (128 * 6) + 114);
            recyclerView.setLayoutParams(layoutParams);
        }

        layoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false);
        recyclerView.setLayoutManager(layoutManager);
        adapter = new ProgrammeImageRvAdapter(mContext, R.layout.layout_programme_detail_image_rv_item, imageList);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter1, View view, int position) {
                EventBus.getDefault().post(new ImageClickEvent(imageList.get(position), position));

                adapter.setSelectPos(position);
                adapter.notifyDataSetChanged();
            }
        });
        recyclerView.setAdapter(adapter);

        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                rvFirstVisibleItem = layoutManager.findFirstVisibleItemPosition();
                rvLastVisibleItem = layoutManager.findLastVisibleItemPosition();
            }
        });
    }

    /**
     * 继承GestureListener，重写left和right方法
     */
//    private class MyGestureListener extends GestureListener {
//        public MyGestureListener(Context context) {
//            super(context);
//        }
//
//        @Override
//        public boolean left(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
//            Log.e("test", "向左滑");
//            if (imageList.size() > 0) {
//                mView.getIvMain().setAnimation(AnimationUtils.loadAnimation(mContext, R.anim.push_left_out1));
//
//                if (adapter.getSelectPos() < imageList.size() - 1) {
//                    Glide.with(mContext)
//                            .load(imageList.get(adapter.getSelectPos() + 1))
//                            .into(mView.getIvMain());
//                    adapter.setSelectPos(adapter.getSelectPos() + 1);
//                    mView.getTvSelect().setText("" + (adapter.getSelectPos() + 1));
//                    adapter.notifyDataSetChanged();
//                }
//
//                new Handler().postDelayed(new Runnable() {
//                    @Override
//                    public void run() {
//                        mView.getIvMain().setAnimation(AnimationUtils.loadAnimation(mContext, R.anim.push_left_in1));
//                    }
//                }, 250);
//
//            }
//            return super.left(e1, e2, velocityX, velocityY);
//        }
//
//        @Override
//        public boolean right(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
//            Log.e("test", "向右滑");
//            if (imageList.size() > 0) {
//                mView.getIvMain().setAnimation(AnimationUtils.loadAnimation(mContext, R.anim.push_right_in1));
//
//                if (adapter.getSelectPos() > 0) {
//                    Glide.with(mContext)
//                            .load(imageList.get(adapter.getSelectPos() - 1))
//                            .into(mView.getIvMain());
//                    adapter.setSelectPos(adapter.getSelectPos() - 1);
//                    mView.getTvSelect().setText("" + (adapter.getSelectPos() + 1));
//                    adapter.notifyDataSetChanged();
//                }
//                new Handler().postDelayed(new Runnable() {
//                    @Override
//                    public void run() {
//                        mView.getIvMain().setAnimation(AnimationUtils.loadAnimation(mContext, R.anim.push_right_out1));
//                    }
//                }, 250);
//
//            }
//            return super.right(e1, e2, velocityX, velocityY);
//        }
//    }
}
